/**
 * @FileName: MemberOrg.java
 * @Author
 * @Description:
 * @Date 2015年12月22日 下午4:53:35
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.model;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.util.InterfaceUtil;
import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import java.util.Date;

@TableBind(tableName = "business_auth_config")
public class BusinessAuthConfig extends Model<BusinessAuthConfig>{
    private static final long  serialVersionUID = 1221507976941242033L;
    public static final BusinessAuthConfig dao              = new BusinessAuthConfig();

    public static final String CONFIG_TYPE = "config_type";
    public static final String CONFIG_TYPE_ID = "config_type_id";
    public static final String CONFIG_BUSINESS = "config_business";
    public static final String CONFIG_BUSINESS_ID = "config_business_id";
    public static final String FLOW_ID = "flow_id";
    public static final String CREATOR_ORG_ID = "creator_org_id";
    public static final String UPDATED_TIME = "updated_time";
    public static final String CITY_ORG_ID = "city_org_id";

    /**
     * 保存一条数据到临时表，具备去重功能，city_org_id不存在就传0或者null，方法内被转换为0
     */
    public void saveATemp(String flow_id,int type,int type_id,Integer city_org_id){
        if(null==city_org_id){
            city_org_id = 0;
        }
        String selectExistData = "select * from " + Constant.Table.TABLE_BUSINESS_AUTH_CONFIG_TEMP
                + " WHERE flow_id=? AND config_type=? AND config_type_id=? AND city_org_id=?";
        Record record = Db.findFirst(selectExistData, flow_id, type, type_id,city_org_id);
        if(record != null && record.getInt("id") > 0){//存在这条数据就不做任何操作
            return;
        }
        String sql = "INSERT INTO " + Constant.Table.TABLE_BUSINESS_AUTH_CONFIG_TEMP + " (" + BusinessAuthConfig.FLOW_ID
                + "," + BusinessAuthConfig.CONFIG_TYPE + "," + BusinessAuthConfig.CONFIG_TYPE_ID + ","+BusinessAuthConfig.CITY_ORG_ID+") VALUES(?,?,?,?)";
        Db.update(sql, flow_id, type, type_id,city_org_id);
    }
    /**
     * 保存一条数据到配置表，具备去重功能，city_org_id不存在就传0或者null，方法内被转换为0
     */
    public void saveAConfig(int type,int type_id,int business,int business_id,Integer city_org_id){
        int creator = InterfaceUtil.getOrgId();
        if(null==city_org_id){
            city_org_id = 0;
        }
        String selectExistData = "SELECT * FROM " + Constant.Table.TABLE_BUSINESS_AUTH_CONFIG
                + " WHERE config_type=? AND config_type_id=? AND config_business=? AND config_business_id=? AND creator_org_id=?  AND city_org_id=?";
        Record record = Db.findFirst(selectExistData, type, type_id, business, business_id,creator, city_org_id);
        if(record != null && record.getInt("id") > 0){
            return;
        }

        Date now = new Date();
        String sql = "INSERT INTO " + Constant.Table.TABLE_BUSINESS_AUTH_CONFIG + " (" + BusinessAuthConfig.CONFIG_TYPE
                + "," + BusinessAuthConfig.CONFIG_TYPE_ID + "," + BusinessAuthConfig.CONFIG_BUSINESS + "," + ""
                + BusinessAuthConfig.CONFIG_BUSINESS_ID + "," + BusinessAuthConfig.CREATOR_ORG_ID + ","
                + BusinessAuthConfig.UPDATED_TIME + "," + BusinessAuthConfig.CITY_ORG_ID+") VALUES(?,?,?,?,?,?,?)";
        Db.update(sql, type, type_id, business, business_id, creator, now,city_org_id);
    }

}
